jQuery("#workflowList").jqGrid({
    datatype: "json",
    url: BASE_URL+"workflow/getList",
    editurl: "workflow/doOperation",
    postData: {dataFormat: "json"},
    colNames:['id','name', 'code', 'memo','created_at', '操作' ],
    colModel:[
    {
        name:'id',
        index:'id',
        editable:false,
        width:55
    },
    {
        name:'name',
        index:'name',
        editable:true,
        formoptions:{elmsuffix:"(*)"},
        editrules:{required:true},
//        editrules:{required:true, custom:true, custom_func:workflowColValidate},
        width:150
    },
    {
        name:'code',
        index:'code',
        editable:true,
        formoptions:{elmsuffix:"(*)"},
        editrules:{required:true},
        width:150,
        align:"left"
    },
    {
        name:'memo',
        index:'memo',
        editable:true,
        width:150,
        edittype:"textarea",
        editoptions:{rows:"2",cols:"10"},
        align:"center"
    },
    {
        name:'created_at',
        index:'created_at',
        sorttype:'date',
        datefmt:'Y-m-d',
        width:220,
        editable:false,
        align:"center"
    },
    {
        name:'act',
        index:'act',
        width:100,
        editable:false,
        sortable:false,
        align:"center"
    }
    ],
    jsonReader : {
        root:"items",
        page: "currentPage",
        total: "totalPages",
        records: "totalRecords",
        userdata: "userdata",
        id: "id",
        repeatitems: false        
    },
    rowNum:10,
    rowList:[10,20,30,40,50],
    sortname: 'id',
    sortorder: "desc",
    multiselect: true, 
    viewrecords: true,
    pager: '#workflowPager',    
    caption:"工作流程列表",
    height: '100%',
    width: '100%',
    
    onSelectRow: function(ids)
    {
        var subItemUrl = BASE_URL + "workflowItem/getList?searchOper=eq&searchField=workflow_id&searchString=";
        if(ids == null)
        {
            ids=0;
            if(jQuery("#workflowItemList").jqGrid('getGridParam','records') >0 )
            {
                jQuery("#workflowItemList").jqGrid('setGridParam',{url:subItemUrl +ids, page:1, _search:1});
                jQuery("#workflowItemList").jqGrid('setCaption',"所属工作流程: "+ids) .trigger('reloadGrid');
            }
        }
        else
        {
            jQuery("#workflowItemList").jqGrid('setGridParam',{url:subItemUrl+ids,page:1, _search:1});
            jQuery("#workflowItemList").jqGrid('setCaption',"所属工作流程: "+ids) .trigger('reloadGrid');
        }
    },

    gridComplete: function(){
        var ids = jQuery("#workflowList").jqGrid('getDataIDs');
        var editUrl = BASE_URL + "workflow/edit";
        var delUrl = BASE_URL + "workflow/delete";
        for(var i=0;i < ids.length;i++){
            var cl = ids[i];
            be = "<input style='height:22px;width:22px;' type='button' value='编' onclick=\"jQuery('#workflowList').jqGrid('editRow','"+cl+"', true, null, workflowCallbackSave, '" + editUrl + "');\" />";
            se = "<input style='height:22px;width:22px;' type='button' value='存' onclick=\"jQuery('#workflowList').jqGrid('saveRow', '"+cl+"', workflowCallbackSave, '" + editUrl + "', {}, null);\" />";
            ce = "<input style='height:22px;width:22px;' type='button' value='消' onclick=\"jQuery('#workflowList').jqGrid('restoreRow', '"+cl+"');\" />";
            de = "<input style='height:22px;width:22px;' type='button' value='删' onclick=\"jQuery('#workflowList').jqGrid('delGridRow', '"+cl+"', {url:'" + delUrl + "'});\" />";
            jQuery("#workflowList").jqGrid('setRowData',ids[i],{
                act:be+se+ce+de
            });
        }
    },

    loadError : function(xhr,st,err){
        alert("Type: "+st+"; Response: "+ xhr.status + " "+xhr.statusText);
    },

    loadComplete: function()
    {
//        console.log($.dump($("#workflowList").jqGrid('getGridParam', 'userData')));
//        console.log("loadComplete");
    }

});
jQuery("#workflowList").jqGrid('navGrid','#workflowPager',
{edit:true, add:true, del:true, search:true, refresh:true, view:true, position:"left"},
{afterSubmit: workflowCallbackValidate, afterComplete: workflowCallbackEdit},
{afterSubmit: workflowCallbackValidate, afterComplete: workflowCallbackAdd},
{afterComplete: workflowCallbackDel},
{afterComplete: workflowCallbackSearch},
{afterComplete: workflowCallbackView}
);

function workflowCallbackValidate(response, postdata)
{
//    console.log("callbackEdit");

    wacAjaxData.response = eval('(' + response.responseText + ')');
//    console.log($.dump(wacAjaxData.response));
//    console.log($.dump(postdata));

    if(wacAjaxData.response.userdata.status == wacOperationStatus.Succss)
    {
        return [true, "", ""];
    }
    else
    {
        return [false, wacAjaxData.response.userdata.error_info];
    }
}

function workflowCallbackSave(response)
{
//    alert("callbackSave");
//    console.log("callbackSave");
//    console.log($.dump($("#workflowList").jqGrid('getGridParam', 'userData')));
//    console.log(response.responseText);
    wacAjaxData.response = eval('(' + response.responseText + ')');
    if(wacAjaxData.response.userdata.status == wacOperationStatus.Succss)
    {
        return true;
    }
    else
    {
        showTips(wacAjaxData.response.userdata.error_info);
        return [false, wacAjaxData.response.userdata.error_info];
    }
}

function workflowCallbackEdit()
{
//    alert("callbackEdit");
//    console.log("callbackEdit");
}

function workflowCallbackAdd()
{
//    alert("callbackAdd");
//    console.log("callbackAdd");
}

function workflowCallbackDel()
{
//    alert("callbackDel");
//    console.log("callbackDel");
}

function workflowCallbackSearch()
{
//    alert("callbackSearch");
//    console.log("callbackSearch");
}

function workflowCallbackView()
{
//    alert("callbackView");
//    console.log("callbackView");
}

function workflowFormValidate(postdata, formid)
{
    $result = [];
    $.getJSON(
        BASE_URL+"workflow/validate",
        postdata,
        function(jsonData){
            wacOperationStatus.Processing = true;
            workflowFormValidateCallBack(jsonData);
        }
    );

   $(".loading").css("display", "block");

   if(wacAjaxData.response.userdata.status == wacOperationStatus.Succss)
   {
       return [true];
   }
   else
   {
       return [false, wacAjaxData.response.userdata.error_info];
   }

//    console.log($.dump(postdata));
    
}

function workflowFormValidateCallBack(jsonData)
{
    wacAjaxData.response = jsonData;
    wacOperationStatus.Processing = false;
    $(".loading").css("display", "none");
    //    console.log($.dump(jsonData));
}